﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.Ordering;

namespace CardLogistics.DAL.Ordering
{
    /// <summary>
    /// 订单版面详细数量表_数据访问类
    /// </summary>
    public class Ordering_DetailedData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_ordering_detailed", StrFields, "ORDERFORMATID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ORDERFORMATID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 初始化时调用版面信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetCardTypeList()
        {
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select FormatId,FormatName from Basic_Format  where STATUS=0 and ISDELETE=0");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["FormatId"].ToString();
                string cardtypename = dt.Rows[i]["FormatName"].ToString();

                cardTypeList.Add(id, cardtypename);
            }
            return cardTypeList;
        }

        /// <summary>
        ///通过外键筛选版面信息
        /// </summary>
        /// <param name="conId">合同外键明细Id</param>
        /// <returns></returns>
        public Dictionary<string, string> GetTypeList(string conId)
        {
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  F.FormatId,F.FormatName from Basic_Format F");
            strSql.Append(" inner join v_purchase_contract_detail C on C.suppid =F.Suppid and C.id='" + conId + "'");
            strSql.Append("  where STATUS=0 and ISDELETE=0");
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(strSql.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["FormatId"].ToString();
                string cardtypename = dt.Rows[i]["FormatName"].ToString();

                cardTypeList.Add(id, cardtypename);
            }
            return cardTypeList;
        }


        /// <summary>
        /// 通过合同Id获取查询条件
        /// <param name="conId">合同外键明细Id</param>
        /// </summary>
        public string strWhere(string conId)
        {
          StringBuilder strSql = new StringBuilder();
          strSql.Append(" and ID in (select CardType from purchase_detailed where DemandId=(select DemandId from purchase_contract where ID=");
          strSql.Append("(select ConId from purchase_contract_detail where ID='" + conId + "')))");
          return strSql.ToString();

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Ordering_Detailed GetModel(string rId)
        {
            Ordering_Detailed model = new Ordering_Detailed();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ORDERFORMATID"] != null)
                {
                    model.ORDERFORMATID = row["ORDERFORMATID"].ToString();
                }
                if (row["FORMATTYPE"] != null)
                {
                    model.FORMATTYPE = row["FORMATTYPE"].ToString();
                }
                if (row["FORMATNUMBER"] != null && row["FORMATNUMBER"].ToString() != "")
                {
                    model.FORMATNUMBER = decimal.Parse(row["FORMATNUMBER"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Ordering_Detailed model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					new OracleParameter("vFORMATTYPE", OracleType.NVarChar),
					new OracleParameter("vFORMATNUMBER", OracleType.Number,4),};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.FORMATTYPE;
                parameters[2].Value = model.FORMATNUMBER;

                string StrOutPar = DbHelperSQL.RunProcedure("P_Ordering_Detailed_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Ordering_Detailed model)
        {
            try
            {
                OracleParameter[] parameters = {
				    new OracleParameter("vORDERFORMATID", OracleType.NVarChar),
					new OracleParameter("vFORMATTYPE", OracleType.NVarChar),
					new OracleParameter("vFORMATNUMBER", OracleType.Number,4),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ORDERFORMATID;
                parameters[1].Value = model.FORMATTYPE;
                parameters[2].Value = model.FORMATNUMBER;
                parameters[22].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Ordering_Detailed_Update", parameters, parameters[22]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Ordering_Detailed_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
